package io.kiki.task.client.job.core.handler;

import cn.hutool.core.lang.Pair;
import io.kiki.task.client.common.exception.ClientException;


public abstract class AbstractRequestHandler<R> implements RequestHandler<R> {

    protected static final String SHARD_NUM = "shardNum";

    /**
     * 具体调用
     *
     * @return
     */
    @Override
    public R execute() {
        Pair<Boolean, String> checked = checkRequest();
        if (checked.getKey()) {
            beforeExecute();
            R r = doExecute();
            afterExecute(r);
            return r;
        } else {
            throw new ClientException("job openapi check error. [{}]", checked.getValue());
        }
    }

    protected abstract void afterExecute(R r);

    protected abstract void beforeExecute();

    protected abstract R doExecute();

    protected abstract Pair<Boolean, String> checkRequest();
}
